#include "mainwindow.h"
#include "qdebug.h"
#include "ui_mainwindow.h"
#include "qpushbutton.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton_1, &QPushButton::clicked, this, [=](){
        file->set_value("aa/88", QApplication::applicationDirPath()+ "/test.ini", 888);
        file->set_value("aa/ww", QApplication::applicationDirPath()+ "/test.ini", "b不知道");

        int a= file->get_value("aa/88", QApplication::applicationDirPath()+ "/test.ini").toInt();
        QString m = file->get_value("aa/ww", QApplication::applicationDirPath()+ "/test.ini").toString();
        qDebug() << a <<m;

        file->create_file(QApplication::applicationDirPath()+ "/test/ee/rr/tt");
    });

    connect(ui->pushButton_2, &QPushButton::clicked, this, [=](){

        QString path = QApplication::applicationDirPath()+ "/test.data";
        QList<QString> list = {"test_1", "KEY", "A INT", "B TEXT", "C TEXT"};
        QList<QString> list_2 = {"test_2", "KEY", "A11 INT", "B11 TEXT NO NULL", "C11 TEXT", "D11 INT", "M11 INT"};


        xsql->open(data, path, "first");
        xsql->create_table(data, list);
        xsql->create_table(data, list_2);
        xsql->rename_table(data, "test_1", "test_rename");
        xsql->delete_table(data, "test");

    });
    connect(ui->pushButton_3, &QPushButton::clicked, this, [=](){

        QSqlDatabase data;
        QString path = QApplication::applicationDirPath()+ "/test.data";

        xsql->open(data, path, "first");
        QList<QVariant> list = {"DF", "id", 2};
        xsql->delete_row(data, list);
        QList<QVariant> list2 = {"DF", "id", 4};
        xsql->delete_row(data, list2);
    });
    connect(ui->pushButton_4, &QPushButton::clicked, this, [=](){

        QDateTime time = QDateTime::currentDateTime();
        QString path = QApplication::applicationDirPath()+ "/test.data";
        QList<QString> list = {"DF", "KEY", "A INT", "B TEXT", "C DATETIME"};
        QList<QVariant> list_ = {"DF",
                                 "A", 4545,
                                 "B", "buvidc",
                                 "C", time};

        xsql->open(data, path, "first");
        xsql->create_table(data, list);
        xsql->insert_row(data, list_);

    });
    connect(ui->pushButton_5, &QPushButton::clicked, this, [=](){

        QDateTime time = QDateTime::currentDateTime();
        QString path = QApplication::applicationDirPath()+ "/test.data";
        xsql->open(data, path, "first");

        QList<QVariant> list = { "DF",
                                 "A", 88888888,
                                 "B", "测试",
                                 "C", time,
                                 "id", 3};
        xsql->update_row(data, list);

        QList<QVariant> list2 = { "DF",
                                 "A", 76776,
                                 "B", "测试",
                                 "id", 5};
        xsql->update_row(data, list2);

    });
    connect(ui->pushButton_6, &QPushButton::clicked, this, [=](){

        QString path = QApplication::applicationDirPath()+ "/test.data";
        xsql->open(data, path, "first");

        QList<QVariant> list = {"DF", "id", 3, 2};
        xsql->rename_id(data, list);

        QList<QVariant> list2 = {"DF", "id", 5, 3};
        xsql->rename_id(data, list2);

    });
    connect(ui->pushButton_7, &QPushButton::clicked, this, [=](){

        QString path = QApplication::applicationDirPath()+ "/test.data";
        xsql->open(data, path, "first");

        QList<QVariant> list = {"DF",
                                "id", "B", "A",
                                "id", 2};
        QList<QVariant> list_ = xsql->get_row(data, list);

        QList<QVariant> list2 = {"DF",
                                 "A", "C", "C",
                                 "id", 3};
        QList<QVariant> list2_ = xsql->get_row(data, list2);

        qDebug() << list_<< "\n" << list2_ ;

    });
    connect(ui->pushButton_8, &QPushButton::clicked, this, [=](){

        QString path = QApplication::applicationDirPath()+ "/test.data";
        xsql->open(data, path, "first");

        int max = xsql->get_max_id(data,"DF");
        int min = xsql->get_min_id(data,"DF");

        qDebug() << min <<max;

    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

